package pro.spring.controller.frontController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pro.spring.model.Users;
import pro.spring.tool.EmailSend;

@Controller
@RequestMapping("email")
public class EmailController extends BaseController{
	
	
	@RequestMapping(value="index.html",method=RequestMethod.GET)
	public String index(){
		return "page/text/email";
	}
	
	@ResponseBody
	@RequestMapping(value="check.json",method=RequestMethod.POST)
	public Integer email(String email,HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer num = 0;
		if(usersService.selectByEmail(email)!=null){
			String pass = emailService.getCheckPass() ;
			EmailSend e =new  EmailSend();
			try {
				num =e.send(email,pass);
			} catch (MessagingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			session.setAttribute(email,pass);
		}
		return num;
	}
	
	@ResponseBody
	@RequestMapping(value="checked.json",method=RequestMethod.POST)
	public Integer emailed(String checkPass,String email,HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		String pass = (String) session.getAttribute(email);
		if(pass.equals(checkPass)){
			Users u  =  usersService.selectByEmail(email);
			session.setAttribute("uName",u.getuName());
			session.setAttribute("uId",u.getuId());
			return  1;
		}else{
			return  1;
		}
		
	}
	
	
}
